import useToastMsg from '@/hooks/sysMsg'

// 引入 UniApp 类型定义（确保项目已正确配置类型）
const toastMsg = useToastMsg()
export const subscribeMsg = (templateIds: string[]) => {
  return new Promise((resolve) => {
    // 定义参数选项，使用正确的 tmplIds 属性名
    uni.requestSubscribeMessage({
      tmplIds: templateIds, // 关键修正：属性名是 tmplIds
      success(res: any) {
        // 遍历所有模板ID，判断用户对每个模板的订阅情况
        templateIds.forEach((tmplId) => {
          switch (res[tmplId]) {
            case 'accept':
              console.log(`用户已同意订阅模板ID为 ${tmplId} 的消息`)
              break
            case 'reject':
              console.log(`用户已拒绝订阅模板ID为 ${tmplId} 的消息`)
              break
            case 'ban':
              console.log(`用户已被禁止订阅模板ID为 ${tmplId} 的消息`)
              break
            case 'filter':
              console.log(`模板ID为 ${tmplId} 的消息被后台过滤`)
              break
            default:
              console.log(`模板ID为 ${tmplId} 的订阅状态未知: ${res[tmplId]}`)
          }
        })
      },
      fail(err: any) {
        console.error('请求订阅消息失败', err)
      },
      complete() {
        resolve(true)
        console.log('请求订阅消息完成')
      },
    })
  })
}


export function isWindowsPlatform(): boolean {
  // uni-app 环境下
  if (typeof uni !== 'undefined' && uni.getSystemInfoSync) {
    const info = uni.getSystemInfoSync();
    if (info.platform === 'windows') return false;
    return true;
  }
  // Web/Electron 环境下
  if (typeof navigator !== 'undefined') {
    const ua = navigator.userAgent.toLowerCase();
    if (ua.includes('windows')) return false;
    if (
      ua.includes('android') ||
      ua.includes('iphone') ||
      ua.includes('ipad') ||
      ua.includes('macintosh') ||
      ua.includes('ios') ||
      ua.includes('miniprogram') ||
      ua.includes('micromessenger')
    ) return true;
  }
  // Node/Electron 环境
  if (typeof process !== 'undefined' && process.platform) {
    if (process.platform === 'win32') return false;
    return true;
  }
  return true;
}